/**
 * 针对后端的设置
 * @author 谢小旭
 */


// jQuery 执行错误处理事件
$.ajaxSetup({
	error: function(request, textStatus, errorThrown){
		$('.ladda-button').ladda('stop');
		$.Notification.notify('error','top center', '服务器异常', request.responseText);
	}
});

//默认DataTable
$.extend($.fn.dataTable.defaults, {
    serverSide: true,
    processing: true,
    searching: false,
    autoWidth: false,
    order: [],
    rowId: 'id',
    aLengthMenu: [
        [10, 20, 50, 100],
        [10, 20, 50, 100]
    ],
    ajax: {
        type: 'post',
        data: function(d, table){
        	var order = $(table.nTable).DataTable().order(), param = {};
        	if(order[0]!=null){
        		param.storeType = order[0][1];
        		param.storeName = d.columns[order[0][0]].data;
          	}else{
          		param.storeType = "desc";
          		param.storeName = table.rowId;
          	}
        	param.length = d.length;
        	param.start = d.start;
        	//$(table.nTable).ajaxParams = $(table.nTable).ajaxParams || {};
          	param = $.extend( param, $(table.nTable).data() || {});
          	return param;
        }
    },
    iDisplayLength: 10,
    language: {
   		url : contextPath + "/assets/plugins/datatables/json/Chinese.json"
    },
    //dom: "t<'row'<'col-md-6'<' pull-left'i><'hidden-xs pull-left m-l-15'l>><'col-md-6'p>>"
    dom: "<'row'<'col-sm-12't>><'row m-t-10'<'col-sm-6 clearfix'<'pull-left'i><'pull-left m-l-15'l>><'col-sm-6'p>>"
});

$(function(){
	
	var $link = $('a.active','#sidebar-menu ul.list-unstyled');
	var $link_parent = $link.closest('.has_sub').children('a');
	var $link_root = $link_parent.closest('ul').children('.menu-title');
	$('.breadcrumb').empty();
	
	var $link_root_text = $link_root.text().trim();
	var $link_parent_text = $link_parent.text().trim();
	var $link_text = $link.text().trim();
	var $link_href = $link.attr('href');
	
	if($link_root_text != ''){
		$('.breadcrumb').append('<li>'+$link_root_text+'</li>');
		sessionStorage.setItem("$link_root_text", $link_root_text);
	} else if(sessionStorage.getItem("$link_root_text")){
		$('.breadcrumb').append('<li>'+sessionStorage.getItem("$link_root_text")+'</li>');
	}
	if($link_parent_text != ''){
		$('.breadcrumb').append('<li>'+$link_parent_text+'</li>');
		sessionStorage.setItem("$link_parent_text", $link_parent_text);
	} else if(sessionStorage.getItem("$link_parent_text")) {
		$('.breadcrumb').append('<li>'+sessionStorage.getItem("$link_parent_text")+'</li>');
	}
	if($link_text != ''){
		$('.breadcrumb').append('<li>'+$link_text+'</li>');
		sessionStorage.setItem("$link_text", $link_text);
		sessionStorage.setItem("$link_href", $link_href);
	} else if(sessionStorage.getItem("$link_text")) {
		$('.breadcrumb').append('<li>'+sessionStorage.getItem("$link_text")+'</li>');
	}
	
	if(sessionStorage.getItem("$link_href")){
		var $link = $('a[href="'+sessionStorage.getItem("$link_href")+'"]','#sidebar-menu');
		$link.addClass('active');
		$link.parent().addClass('active');
		$link.closest('.list-unstyled').show();
		var $link_parent = $link.closest('.has_sub').children('a');
		$link_parent.addClass('active').addClass('subdrop');
	}
});

function removeLinkSessionStorage(){
	sessionStorage.removeItem("$link_root_text");
	sessionStorage.removeItem("$link_parent_text");
	sessionStorage.removeItem("$link_text");
	sessionStorage.removeItem("$link_href");
}


